package evan.zhang.flags;


import java.util.ArrayList;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay;

public class FlagsBalloonItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> {

    private final static String tag = "FlagsOnMap::MyBalloonItemizedOverlay";
	private ArrayList<OverlayItem> overlay;
	private int hasMove;
	private boolean isItemsLoaded;
	private Flags flags;
	//private boolean isLoaded = false;
	//private static final MyBalloonItemizedOverlay vInstance = new MyBalloonItemizedOverlay(null);
	
	public FlagsBalloonItemizedOverlay(Drawable defaultMarker, FlagsContext flagsContext) {
		super(defaultMarker,flagsContext);
		//myContext = mapView.getContext();
		this.flags = flagsContext.getFlags();
		this.overlay = new ArrayList<OverlayItem>(flags.getNumberOfItem());
		this.hasMove = 0;
		this.isItemsLoaded = false;
	}
	
	/*
	 * Added by Evan Zhang
	 */
	public boolean showBalloon() {
		Log.e(tag, "showBalloon is called");
		
		super.getBalloonView().setData(flags);
		MapView.LayoutParams params = new MapView.LayoutParams(
				LayoutParams.WRAP_CONTENT, 
				LayoutParams.WRAP_CONTENT, 
				flags.getFlagInfo().getGeoPoint(),
				MapView.LayoutParams.BOTTOM_CENTER);
		params.mode = MapView.LayoutParams.MODE_MAP;
		
		//setBalloonTouchListener(thisIndex);
		
		super.getBalloonView().setLayoutParams(params);
		super.getMapContext().getMapView().getController().setCenter(flags.getFlagInfo().getGeoPoint());
		//super.getMapContext().getMapView().getController().animateTo(flags.getFlagInfo().getGeoPoint());
		
		//hard coded to zoom level 5
		super.getMapContext().getMapView().getController().setZoom(5);
		//super.getMapContext().getMapView().getController().zoomToSpan(flags.getFlagInfo().getLatSpan(), flags.getFlagInfo().getLonSpan());
		//Log.d(tag, "super.getBalloonView().getVisibility() = "+String.valueOf(super.getBalloonView().getVisibility()));
		super.getBalloonView().setVisibility(View.VISIBLE);
		//Log.d(tag, "super.getBalloonView().getVisibility() = "+String.valueOf(super.getBalloonView().getVisibility()));
		return true;
		
	}
	/*
	protected boolean onBalloonTap(int index) {
		//Toast.makeText(myContext, "onBalloonTap for overlay index " + index, Toast.LENGTH_LONG).show();
		return true;
	}
	 */
	public void loadItemsOnce(){	
		if (this.isItemsLoaded) {
			return;
		}
		
		int i;
		for (i=0; i<flags.getNumberOfItem(); i++) {
			Log.e(tag, "item "+String.valueOf(i)+" is loaded");
			/*
			OverlayItem item = new OverlayItem(flags.getGeoPoints().get(i),
					                           flags.getDistsDefault().get(i)+"  "+flags.getDistsLocal().get(i),
					                           flags.getCapitalsDefault().get(i)+"  "+flags.getCapitalsLocal().get(i));
	        */
	        OverlayItem item = new OverlayItem(flags.getGeoPoints().get(i),"","");        
			this.addOverlayItem(item, flags.getTinyFlags().get(i));	
	        
		}
		this.isItemsLoaded = true;
	}
	
	public void addOverlayItem(OverlayItem item, Drawable marker) {
		item.setMarker(boundCenter(marker));
		overlay.add(item);
		populate();
	}
	

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return overlay.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return overlay.size();
	}

	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
		
		//@hasMove will be manipulated in this function
		
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			//Toast.makeText(vContext, "action_down ",Toast.LENGTH_LONG).show();
			Log.d(tag,"action_down");
			hasMove = 0;
			return false;
		}
		else if (event.getAction() == MotionEvent.ACTION_UP) {
			//Toast.makeText(vContext, "action_up ",Toast.LENGTH_LONG).show();
			Log.d(tag,"action_up");
			if (hasMove == 0) {
				//hideBalloon();
			}
			return false;
		}
		else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			//Toast.makeText(vContext, "action_move ",Toast.LENGTH_LONG).show();
			Log.d(tag,"action_move");
			hasMove = 1;
			return false;
		}
		else {
			return false;
		}
	}
	
    
}
